package jomm.utils;

import org.apache.log4j.Logger;

import java.util.*;

/**
 * Class Description
 *
 * @author Jorge Machado <machadofisher@gmail.com>
 * 
 */
public class PropertiesUtils
{
    private static final Logger logger = Logger.getLogger(PropertiesUtils.class);

    /**
     *
     * @param p properties in use
     * @param propertyPrefix to find
     * @return a list of all property names hat starts with the given prefix
     *
     */
    public static List<String> getListProperties(Properties p, String propertyPrefix)
    {

        List<String> props = new ArrayList<String>();
        Enumeration<?> enumE;
        try
        {
            enumE = p.propertyNames();
        }
        catch(Exception e)
        {
            PropertiesUtils.logger.fatal("cant load properties");
            return null;
        }

        while (enumE.hasMoreElements())
        {
            String propertyName = (String)enumE.nextElement();
            if (propertyName.startsWith(propertyPrefix))
            {
                props.add(propertyName);
            }
        }
        Collections.sort(props, StringComparator.getInstance());
        return props;
    }

    /**
     * @param p to use
     * @param propertyPrefix to find
     * @return a list of values
     */
    public static List<String> getListValuesOrderedByPropertyName(Properties p, String propertyPrefix)
    {
        List<String> props = new ArrayList<String>();
        List<String> properties = PropertiesUtils.getListProperties(p,propertyPrefix);
        for (Object property1 : properties)
        {
            String property = (String) property1;
            props.add(p.getProperty(property));
        }
        return props;
    }

    /**
     * @param p to use
     * @param propertyPrefix to find
     * @return a list of values
     */
    public static List<String> getListValuesOrderedByValue(Properties p, String propertyPrefix)
    {
        List<String> props = new ArrayList<String>();
        List<String> properties = PropertiesUtils.getListProperties(p,propertyPrefix);
        for (Object property1 : properties)
        {
            String property = (String) property1;
            props.add(p.getProperty(property));
        }
        Collections.sort(props,StringComparator.getInstance());
        return props;
    }


    /**
     * Get property and convert it to float
     *
     * @param property to get and convert
     * @return float value
     * @param props to use
     */
    public static float getFloatProperty(String property, Properties props)
    {
        String stringValue = props.getProperty(property);
        float floatValue = 0;

        if (stringValue != null)
        {
            try
            {
                floatValue = new Float(stringValue);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
        }
        return floatValue;
    }

    /**
     * Get property and convert it to boolean
     *
     * @param property to get and convert
     * @return float value
     * @param props to use
     */
    public static boolean getBooleanProperty(String property, Properties props)
    {
        String stringValue = props.getProperty(property);
        boolean booleanValue = false;

        if (stringValue != null)
        {
            try
            {
                booleanValue = Boolean.parseBoolean(stringValue);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return booleanValue;
    }

    /**
     * Get property and convert it to float
     *
     * @param property to get and convert
     * @return float value
     * @param props to use
     */
    public static short getShortProperty(String property, Properties props)
    {
        String stringValue = props.getProperty(property);
        short shortValue = 0;

        if (stringValue != null)
        {
            try
            {
                shortValue = new Short(stringValue);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
        }
        return shortValue;
    }

    /**
     * Get property and convert it to float
     *
     * @param property to get and convert
     * @return float value
     * @param props to use
     */
    public static int getIntProperty(String property, Properties props)
    {
        String stringValue = props.getProperty(property);
        int intValue = 0;

        if (stringValue != null)
        {
            try
            {
                intValue = new Integer(stringValue);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
        }
        return intValue;
    }

}
